package com.example.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.log.InitLog;
import com.example.log.UpdateLog;
import com.example.mapper.BusNewsMapper;
import com.example.pojo.BusNews;
import com.example.pojo.PageBean;
import com.example.pojo.Reservation;
import com.example.pojo.Result;
import com.example.service.impl.BusNewsServerImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class BusNewsService extends ServiceImpl<BusNewsMapper, BusNews> implements BusNewsServerImpl {
    @Autowired
    private BusNewsMapper busNewsMapper;
    public Result getList(){
        List<BusNews> busNews = busNewsMapper.selectList(new LambdaQueryWrapper<BusNews>()
                .between(BusNews::getId, 0, 5));
        return Result.succeed(busNews);
    }

    public Result pageList(PageBean pageBean, String name) {
        int currentPage = pageBean.getPage();
        int pageSize = pageBean.getPageSize();
        //关键词模糊匹配,逻辑删除
        LambdaQueryWrapper<BusNews> wrapper=new LambdaQueryWrapper<BusNews>()
                .like(name!=null&&!name.isEmpty(),BusNews::getTitle,name)
                .eq(BusNews::getIsDeleted,0);
        Page<BusNews> page=new Page<>(currentPage,pageSize);
        page(page,wrapper);
        List<BusNews> busNews = page.getRecords();
        return Result.succeed(busNews);
    }
    @InitLog
    public Result addBusNews(BusNews busNews) {
        save(busNews);
        return Result.succeed();
    }

    @Override
    public Result deleteByIds(List<Integer> ids) {
        update(new LambdaUpdateWrapper<BusNews>()
                .in(BusNews::getId,ids)
                .set(BusNews::getUpdateTime, LocalDateTime.now())
                .set(BusNews::getIsDeleted,1));
        return Result.succeed();
    }
    @UpdateLog
    @Override
    public Result updateBusNews(BusNews busNews) {
        saveOrUpdate(busNews,new LambdaUpdateWrapper<BusNews>()
                .eq(BusNews::getId,busNews.getId()));
        return Result.succeed();
    }
}
